package main

import (
	"fmt"
	"net"
	"os"
)

func main() {

	clientConn, clientErr := net.Dial("tcp", "127.0.0.1:6666") //主动连接服务器
	if clientErr != nil {
		fmt.Println("err = ", clientErr)
		return
	}

	defer clientConn.Close()

	// //发送数据
	// clientConn.Write([]byte(" linlin , are u ok?"))
	go func() {
		str := make([]byte, 1024)
		for {
			keyIndex, keyErr := os.Stdin.Read(str)
			if keyErr != nil {
				fmt.Println(" os.Stdin.err = ", keyErr)
				return
			}
			clientConn.Write(str[:keyIndex])
		}

	}()

	buffer := make([]byte, 1024)
	for {
		serverIndex, serverErr := clientConn.Read(buffer)
		if serverErr != nil {
			fmt.Println(" conn.Read Err = ", serverErr)
			return
		}
		fmt.Println(" server data : ", string(buffer[:serverIndex]))
	}

}
